package mulMan.services;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.dao.DataAccessException;
import org.springframework.transaction.annotation.Transactional;

import mulMan.dao.AudioFileDAO;
import mulMan.domain.AudioFile;

public class AudioFileServicesImpl implements AudioFileServices, ApplicationContextAware {
	
	private AudioFileDAO audioFileDAO;
	private ApplicationContext applicationContext;
	
	public void setAudioFileDAO(AudioFileDAO audioFileDAO) {
		this.audioFileDAO = audioFileDAO;
	}
	
	
	@Transactional
	public void saveAudioFile(AudioFile audioFile) throws Exception {
		try {
			audioFileDAO.saveAudioFile(audioFile);
			System.out.println("Audio file inserted with [id: " + audioFile.getIdAudioFile() + 
					"] [title: " + audioFile.getTitle() +
					"] [album: " + audioFile.getAlbum() +
					"] [artist: " + audioFile.getArtist() +
					"] [length: " + audioFile.getLength() +
					"] [path: " + audioFile.getPath() + "]");
		} catch (DataAccessException dae) {
			throw new Exception("An error occured while trying to insert an audio file into the database:" + dae);
		}
	}

	@Transactional
	public List<AudioFile> getAudioFiles() throws Exception{
		try {
			List<AudioFile> audioFileList = audioFileDAO.getAudioFiles();
			
			for (AudioFile audioFile : audioFileList) {
				audioFile.setAudioFileServices((AudioFileServices) applicationContext.getBean("audioFileServices"));
			}
			
			System.out.println(audioFileList.size() + " audio file records recovered from the database");
			return audioFileList;
		} catch (DataAccessException dae) {
			throw new Exception("An error occured while trying to get the list of audio files from the database:" + dae);
		}
	}

	@Transactional
	public AudioFile getAudioFile(int idAudioFile) throws Exception {
		try {
			AudioFile audioFile = audioFileDAO.getAudioFile(idAudioFile);

			audioFile.setAudioFileServices((AudioFileServices) applicationContext.getBean("audioFileServices"));
			
			System.out.println("Audio file record recovered from the database with [id: " + audioFile.getIdAudioFile() + 
					"] [title: " + audioFile.getTitle() +
					"] [album: " + audioFile.getAlbum() +
					"] [artist: " + audioFile.getArtist() +
					"] [length: " + audioFile.getLength() +
					"] [path: " + audioFile.getPath() + "]");
			
			return audioFile;
		} catch (DataAccessException dae) {
			throw new Exception("An error occured while trying to get the audio file from the database:" + dae);
		}
	}

	@Transactional
	public void updateAudioFile(AudioFile audioFile) throws Exception {
		try {
			audioFileDAO.updateAudioFile(audioFile);
			System.out.println("Audio file updated with [id: " + audioFile.getIdAudioFile() + 
					"] [title: " + audioFile.getTitle() +
					"] [album: " + audioFile.getAlbum() +
					"] [artist: " + audioFile.getArtist() +
					"] [length: " + audioFile.getLength() +
					"] [path: " + audioFile.getPath() + "]");
		} catch (DataAccessException dae) {
			throw new Exception("An error occured while trying to update an audio file into the database:" + dae);
		}
	}

	@Transactional
	public void deleteAudioFile(AudioFile audioFile) throws Exception {
		try {
			audioFileDAO.deleteAudioFile(audioFile);
			System.out.println("Audio file successfuly deleted");
		} catch (DataAccessException dae) {
			throw new Exception("An error occured while trying to delete an audio file from the database:" + dae);
		}
	}


	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		if (applicationContext == null) {
			applicationContext = arg0;
		}
	}

}
